package com.sky.service.user.impl;

import cn.hutool.core.date.DateException;
import cn.hutool.db.sql.Order;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.mapper.user.*;
import com.sky.pojo.AddressBook;
import com.sky.pojo.OrderDetail;
import com.sky.pojo.Orders;
import com.sky.pojo.ShoppingCart;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.user.OrdersSubmitService;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class OrdersSubmitServiceImpl implements OrdersSubmitService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public OrderSubmitVO submit(OrdersSubmitDTO dto) {
        if (null==dto||null==dto.getAddressBookId()){
            throw new DateException(MessageConstant.DATA_ERROR);
        }
        Orders orders = new Orders();
        BeanUtils.copyProperties(dto,orders);

         AddressBook addressBook = addressBookMapper.selectById(orders.getAddressBookId());

        orders.setNumber(UUID.randomUUID().toString().replaceAll("-",""));
        orders.setAddress(addressBook.getProvinceName()+addressBook.getCityName()+addressBook.getDistrictName()+addressBook.getDetail());
        orders.setOrderTime(LocalDateTime.now());
        orders.setStatus(Orders.PENDING_PAYMENT);
        orders.setUserId(BaseContext.getCurrentId());
        orders.setPayStatus(Orders.UN_PAID);
        orders.setConsignee(addressBook.getConsignee());
        orders.setPhone(addressBook.getPhone());

        ordersMapper.save(orders);
        List<ShoppingCart>list = shoppingCartMapper.selectByUserId(BaseContext.getCurrentId());
        List<OrderDetail> orderDetails = new ArrayList<>();
        for (ShoppingCart cart : list) {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(cart,orderDetail);
            orderDetail.setOrderId(orders.getId());
            orderDetails.add(orderDetail);
        }
        log.info("详细订单集合:{}",orderDetails);
        orderDetailMapper.save(orderDetails);


        OrderSubmitVO vo =OrderSubmitVO.builder()
                .id(orders.getId())
                .orderAmount(orders.getAmount())
                .orderNumber(orders.getNumber())
                .orderTime(orders.getOrderTime())
                .build();
        return vo;
    }

    @Override
    public PageResult historyOrders(OrdersPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
       Page<OrderVO> page = ordersMapper.selectOrders(dto);
        for (OrderVO orderVO : page) {
            Long orderId = orderVO.getId();
            List<OrderDetail> orderDetail = orderDetailMapper.selectByOrderId(orderId);
            orderVO.setOrderDetailList(orderDetail);
        }
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    public Result<OrderVO> selectByOrderId(Long orderId) {
       OrderVO orderVO = ordersMapper.selectById(orderId);
        List<OrderDetail> orderDetails = orderDetailMapper.selectByOrderId(orderId);
        orderVO.setOrderDetailList(orderDetails);
        return Result.success(orderVO);

    }
    @ApiOperation("再来一单")
    @Override
    public void saveShoppingById(Long id) {
        List<OrderDetail> orderDetails = orderDetailMapper.selectByOrderId(id);
        ArrayList<ShoppingCart> shoppingCarts = new ArrayList<>();
        for (OrderDetail orderDetail : orderDetails) {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(orderDetail,shoppingCart);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCarts.add(shoppingCart);
        }
        shoppingCartMapper.insert(shoppingCarts);
    }
}
